<template>
  <div class="roadSectionManagement-page">
    <breadcrumb />
    <div class="search-container">
      <Search @search="getSearchParams" />
    </div>
    <div class="table-container">
      <div class="table-component">
        <div class="submit-btns">
          <div></div>
          <div class="submit-btns-right">
            <el-button type="primary" class="submit-btn-add" @click="addData">
              <img class="icon-img" :src="iconAdd" alt="" />
              新增</el-button
            >
            <el-button
              type="primary"
              class="submit-btn-import"
              @click="importData"
            >
              <img class="icon-img" :src="iconImport" alt="" />导入</el-button
            >
          </div>
        </div>
        <div class="table">
          <el-table
            :data="table.data"
            :loading="table.loading"
            style="width: 100%"
            :header-cell-style="{
              background: '#F1F1F1',
              'font-size': '14px',
              'font-weight': 600,
              color: '#333333',
            }"
            :cell-style="{
              'font-size': '14px',
              'font-weight': 400,
              color: '#333333',
            }"
          >
            <el-table-column label="序号" min-width="60" fixed="left">
              <template #default="scope">
                {{ (scope.$index + 1).toString().padStart(2, "0") }}
              </template>
            </el-table-column>
            <el-table-column prop="code" label="路段编号" />
            <el-table-column prop="section" label="路段名称" min-width="120" />
            <!-- <el-table-column prop="region" label="所属行政区" min-width="100" /> -->
            <el-table-column prop="category" label="路段类别" min-width="100">
              <template #default="scope">
                {{
                  scope.row.category == 1
                    ? "一类路段"
                    : scope.row.category == 2
                    ? "二类路段"
                    : "三类路段"
                }}
              </template>
            </el-table-column>
            <el-table-column
              prop="berthNum"
              label="泊位数（个）"
              min-width="120"
            />
            <el-table-column label="泊位分布">
              <template #default="scope">
                {{ scope.row.distribution == 1 ? "单侧" : "双侧" }}
              </template>
            </el-table-column>
            <el-table-column label="泊位形式">
              <template #default="scope">
                {{
                  scope.row.berthForm == 1
                    ? "一字型"
                    : scope.row.berthForm == 2
                    ? "非字型"
                    : "斜非型"
                }}
              </template>
            </el-table-column>
            <el-table-column label="操作" width="110" fixed="right">
              <template #default="scope">
                <el-button
                  @click="editData(scope.row)"
                  type="primary"
                  size="small"
                  link
                  style="color: #007aff; font-size: 14px"
                  >编辑</el-button
                >
                <el-button
                  @click="deleteData(scope.row)"
                  type="primary"
                  size="small"
                  link
                  style="color: #007aff; font-size: 14px"
                  >删除</el-button
                >
              </template>
            </el-table-column>
            <template #empty>
              <el-empty :image="imgNone" />
            </template>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            v-model:current-page="pagination.currentPage"
            v-model:page-size="pagination.pageSize"
            :page-sizes="pagination.pageSizes"
            layout="total,sizes, prev, pager, next, jumper"
            :total="table.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
        <el-dialog
          v-model="dialogVisible"
          :title="title"
          destroy-on-close
          :before-close="handleClose"
          :close-on-click-modal="false"
          :width="componentTag === importComponent ? '800px' : '520px'"
        >
          <component
            :is="componentTag"
            @operationDone="operationDone"
            :operationId="operationId"
          >
          </component>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script setup>
import Search from "./components/search.vue";
import breadcrumb from "@/components/breadcrumb/index.vue";
import iconAdd from "@/assets/image/btn/icon-add.png";
import iconImport from "@/assets/image/btn/icon-import.png";
const imgNone = new URL("@/assets/image/imgNone.png", import.meta.url).href;
import addComponent from "./components/addComponent.vue";
import editComponent from "./components/editComponent.vue";
import importComponent from "./components/importComponent.vue";
import { ref, reactive, shallowRef } from "vue";
import {
  getroadSection,
  getDeleteRoadSection,
} from "./request.js";

// 搜索
const searchModel = ref({
  keyword: "",
  regionId: "",
});

const getSearchParams = (value) => {
  searchModel.value = value;
  // console.log(value);
  getData();
};

// 弹窗
const title = ref("");
const dialogVisible = ref(false);
const componentTag = shallowRef("");
const operationId = ref("");
const operationDone = (r) => {
  dialogVisible.value = false;
  operationId.value = "";
  if (!r) {
    getData();
  }
};

// 新增按钮
const addData = () => {
  title.value = "新增路段信息";
  componentTag.value = addComponent;
  dialogVisible.value = true;
};

// 编辑按钮
const editData = (row) => {
  title.value = "编辑路段信息";
  operationId.value = row;
  componentTag.value = editComponent;
  dialogVisible.value = true;
};

// 导入
const importData = () => {
  title.value = "导入路段信息";
  componentTag.value = importComponent;
  dialogVisible.value = true;
};

// 删除按钮
const deleteData = (row) => {
  ElMessageBox.confirm(
    `您确认要删除路段 [${row.section}${row.code}]?`,
    "温馨提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  ).then(async () => {
    if (row.berthNum === 0) {
      await getDeleteRoadSection(row.id).then((res) => {
        if (res.data.code === 0) {
          getData();
          ElMessage({
            type: "success",
            message: `已删除路段 [${row.section}${row.code}]`,
          });
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      ElMessage.error("该片区已分配路段，不能删除");
    }
  });
};

// 表格
const table = reactive({
  data: [],
  total: 0,
  loading: true,
});

const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  pageSizes: [10, 20, 30, 50],
});

const handleCurrentChange = (value) => {
  pagination.currentPage = value;
  getData();
};

const handleSizeChange = (value) => {
  pagination.pageSize = value;
  getData();
};

const getData = async () => {
  await getroadSection({
    keyword: searchModel.value.keyword,
    regionId: searchModel.value.regionId,
    pageNo: pagination.currentPage,
    pageSize: pagination.pageSize,
  }).then((res) => {
    if (res.data.code === 0) {
      table.data = res.data.result.items;
      table.total = parseInt(res.data.result.total);
      table.loading = false;
    }
  });
};

getData();
</script>

<style lang="less" scoped>
.roadSectionManagement-page {
  .search-container {
    margin-top: 16px;
    min-height: 100%;
    padding: 24px 24px 8px 24px;
    background: #fff;
    border-radius: 6px;
  }
  .table-container {
    margin-top: 16px;
    min-height: 100%;
    padding: 24px;
    background: #fff;
    border-radius: 6px;
  }
  .table-component {
    .submit-btns {
      display: flex;
      justify-content: space-between;
      margin-bottom: 24px;
    }
    .submit-btns-right {
      display: flex;
    }
    .submit-btn-add {
      background: #007aff;
      border: 1px solid #007aff;
      border-radius: 2px;
      margin-left: 87px;
      padding: 5px 16px;
    }
    .submit-btn-import {
      background: #00d1ff;
      border: 1px solid #00d1ff;
      border-radius: 2px;
      margin-left: 12px;
      padding: 5px 16px;
    }

    .icon-img {
      width: 16px;
      height: 16px;
      margin-right: 8px;
    }
    .table {
      margin-bottom: 15px;
    }
    .pagination {
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }
}
</style>